import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/finish_in.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/store_util.dart';

class FinishedInDialog extends StatefulWidget {
  FinishedInDialog();

  @override
  _FinishedInDialogState createState() => _FinishedInDialogState();
}

class _FinishedInDialogState extends State<FinishedInDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  FinishedIn finishedIn = FinishedIn();
//车间
  List<SelectOptionVO> workshopList = [];
  //源库房
  List<SelectOptionVO> locationList = [];
  @override
  void initState() {
    super.initState();
    _loadData();
  }

  _loadData() async {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    if (userInfo.major != null) {
      workshopList = await DynamicDataApi.getModelSelect(
          model: "hr.department",
          name: "complete_name",
          key: "parent_id",
          operator: "!=",
          val: null);
    } else {
      locationList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: 'workshop_id',
          val: 22,
          key2: 'comment',
          val2: "CPK");
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();

    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          userInfo.major != null
              ? CrySelect(
                  label: '生产车间',
                  dataList: workshopList,
                  value: finishedIn.workshopId,
                  onSaved: (v) {
                    finishedIn.workshopId = v;
                  },
                  onChange: (v) async {
                    if (finishedIn.workshopId != v) {
                      formKey.currentState!.save();
                      finishedIn.workshopId = v;
                      finishedIn.locationId = null;
                      try {
                        locationList = await DynamicDataApi.getModelSelect(
                            active: false,
                            model: "stock.location",
                            name: "complete_name",
                            key: 'workshop_id',
                            val: int.parse(v),
                            key2: 'comment',
                            val2: "CPK");
                      } catch (e) {
                        locationList = [];
                      }
                      setState(() {});
                    }
                  },
                )
              : CryInput(
                  label: '生产车间',
                  enable: false,
                  value: userInfo.deptName,
                ),
          CrySelect(
            label: '库房',
            dataList: locationList,
            value: finishedIn.locationId,
            onSaved: (v) {
              finishedIn.locationId = v;
            },
          ),
          CryInput(
            label: '生产订单号',
            value: finishedIn.productorderno,
            onSaved: (v) {
              finishedIn.productorderno = v;
            },
          ),
          CryInput(
            label: '物料名称',
            value: finishedIn.productName,
            onSaved: (v) {
              finishedIn.productName = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, finishedIn);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("扫码"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
